import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import User from '../views/User.vue'
import Role from '../views/Role.vue'
import Menu from '../views/Menu.vue'
import test from '../views/test.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/layout',
    name: 'Layout',
    component: () => import('../views/Layout.vue'),
    children: [{
      path: '/user',
      name: 'User',
      component: User
    },
    {
      path: '/role',
      name: 'Role',
      component: Role
    },
    // {
    //   path: '/menu',
    //   name: 'Menu',
    //   component: Menu
    // },
    {
      path: '/predictParam',
      name: 'PredictParam',
      component: () => import('../views/PredictParam')
    },
      {
        path: '/datachinacity',
        name: 'datachinacity',
        component: () => import('../views/datachinacity')
      },
    {
      path: '/test',
      name: 'test',
      component: test
    }],
  },

  {
    path: '/bigScreen',
    name: 'index',
    component: () => import('../views/index.vue')
  },
  {
    path: '/dag',
    name: 'dag',
    component: () => import('../views/diagramExample/diagramExample')
  },
  // {
  //   path: '/demo1',
  //   name: 'demo1',
  //   component: () => import('../views/part/demo1')
  // }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  const currentUser = window.sessionStorage.getItem("user");
  if(to.name == "Login"){
    next()
  } else{
    // 未登录重置路径
    if (!JSON.parse(currentUser)) {
      alert('暂未登录，请您先去登录!')
      next('/login')
    }else {
      next()
    }
  }
})


export default router
